package quq.missq.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.ArrayList;
import java.util.LinkedList;

import quq.missq.Constants;
import quq.missq.R;
import quq.missq.activity.TopicDetailActivity;
import quq.missq.beans.TopicBean;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.StringUtils;
import quq.missq.utils.VolleyTool;
import quq.missq.views.NoScrollGridView;

/**
 * Created by River on 8/18/15.
 */
public class MyHomePagerAdapter extends BaseAdapter {

    private LinkedList<TopicBean.TopicResult> results;
    private Context context;

    public MyHomePagerAdapter(Context context) {
        this.context = context;
    }

    public void setData(LinkedList<TopicBean.TopicResult> results) {
        this.results = results;
    }

    public LinkedList<TopicBean.TopicResult> getData() {
        return this.results;
    }

    @Override
    public int getCount() {
        return results.size();
    }

    @Override
    public Object getItem(int position) {
        return results.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final TopicBean.TopicResult result = results.get(position);
        Holder holder;
//        if (convertView == null) {
            holder = new Holder();
            View view = LayoutInflater.from(context).inflate(R.layout.my_homepage_adapter, null);
            holder.time = (TextView) view.findViewById(R.id.time);
            //
            holder.gridView = (NoScrollGridView) view.findViewById(R.id.gridView);


            holder.avatorLayout1 = (RelativeLayout) view.findViewById(R.id.avtarLayout1);
            holder.avatorLayout2 = (RelativeLayout) view.findViewById(R.id.avtarLayout2);
            holder.avatorLayout3 = (RelativeLayout) view.findViewById(R.id.avtarLayout3);
            holder.avtar1 = (NetworkImageView) view.findViewById(R.id.avtar1);
            holder.avtar2 = (NetworkImageView) view.findViewById(R.id.avtar2);
            holder.avtar3 = (NetworkImageView) view.findViewById(R.id.avtar3);
            holder.joinCount = (TextView) view.findViewById(R.id.joinCount);
            holder.comment = (TextView) view.findViewById(R.id.comment);
            holder.content = (TextView) view.findViewById(R.id.content);
            holder.typeIcon = (ImageView) view.findViewById(R.id.topicType);

            holder.imageView = (ImageView) view.findViewById(R.id.imageView);

            holder.commentLayout = (LinearLayout) view.findViewById(R.id.commentLayout);
            holder.commentLayout1 = (LinearLayout) view.findViewById(R.id.commentLayout1);
            holder.tsType = (ImageView) view.findViewById(R.id.tsType);
//            convertView.setTag(holder);
//        } else {
//            holder = (Holder) convertView.getTag();
//        }
        String friendly_time = StringUtils.friendly_time(result.getCreated());
        holder.time.setText(friendly_time);
        // 当category=123的时候 用一张图 其他的用gridview
        int categoryId = result.getCategory().getId();

        if (categoryId == 1 || categoryId == 2 || categoryId == 3) {
            holder.tsType.setImageResource(R.mipmap.ts_dazhao);
            holder.imageView.setVisibility(View.VISIBLE);
            holder.gridView.setVisibility(View.GONE);


            ImageLoadUtil.loadImage(holder.imageView, Constants.IMAGE_HOST + result.getAttachs().get(0).getThumbnail(), R.mipmap.empty_photo);

        } else {
            holder.tsType.setImageResource(R.mipmap.ts_huati);
            holder.imageView.setVisibility(View.GONE);
            holder.gridView.setVisibility(View.VISIBLE);
            if (result.getAttachs().size() > 0) {

                holder.gridView.setAdapter(new ImageGridViewAdapter(context, result.getAttachs()));


            }
        }
        holder.content.setText(result.getTitle());
        int type = -1;
        if (categoryId == 1) {
            type = R.mipmap.icon_plan;
        } else if (categoryId == 2) {
            type = R.mipmap.icon_change;
        } else if (categoryId == 3) {
            type = R.mipmap.icon_night;
        } else if (categoryId == 5) {
            type = R.mipmap.icon_qa;
        } else {
            type = R.mipmap.icon_show;
        }
        holder.typeIcon.setImageResource(type);
        showComments(holder, result.getDigestComments());
        holder.gridView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    Intent intent = new Intent();
                    intent.setClass(context, TopicDetailActivity.class);
                    intent.putExtra("id", result.getId());
                    context.startActivity(intent);
                    return false;
                }
                return false;
            }
        });
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(context, TopicDetailActivity.class);
                intent.putExtra("id", result.getId());
                context.startActivity(intent);
            }
        });
        return view;
    }

    private static final String JOIN_COUNT = "人参与了此问答";

    private void showComments(Holder holder, ArrayList<TopicBean.DigestComment> comments) {
        int counts = comments.size();
        String comment = "";
        if (counts == 0) {
            holder.avatorLayout1.setVisibility(View.GONE);
            holder.avatorLayout2.setVisibility(View.GONE);
            holder.avatorLayout3.setVisibility(View.GONE);
        }
        if (counts == 1) {
            comment = comments.get(0).getContent();
            showLayout1(holder, comments);
            holder.avatorLayout2.setVisibility(View.GONE);
            holder.avatorLayout3.setVisibility(View.GONE);
        } else if (counts == 2) {
            comment = comments.get(0).getContent();
            showLayout1(holder, comments);
            showLayout2(holder, comments);
            holder.avatorLayout3.setVisibility(View.GONE);
        } else if (counts == 3) {
            comment = comments.get(0).getContent();
            showLayout1(holder, comments);
            showLayout2(holder, comments);
            showLayout3(holder, comments);
        }
        holder.comment.setText(comment);
        if (counts > 0) {
            holder.commentLayout1.setVisibility(View.VISIBLE);
            holder.commentLayout.setVisibility(View.VISIBLE);
            holder.joinCount.setVisibility(View.VISIBLE);
            holder.joinCount.setText(counts + JOIN_COUNT);
        } else {
            holder.commentLayout1.setVisibility(View.GONE);
            holder.commentLayout.setVisibility(View.GONE);
            holder.joinCount.setVisibility(View.GONE);
        }

    }

    private void showLayout1(Holder holder, ArrayList<TopicBean.DigestComment> comments) {
        holder.avatorLayout1.setVisibility(View.VISIBLE);
        holder.avtar1.setDefaultImageResId(R.mipmap.empty_photo);
        holder.avtar1.setImageUrl(Constants.IMAGE_HOST + comments.get(0).getAuthor().getAvatar(), VolleyTool.getImageLoader());
    }

    private void showLayout2(Holder holder, ArrayList<TopicBean.DigestComment> comments) {
        holder.avatorLayout2.setVisibility(View.VISIBLE);
        holder.avtar2.setDefaultImageResId(R.mipmap.empty_photo);
        holder.avtar2.setImageUrl(Constants.IMAGE_HOST + comments.get(1).getAuthor().getAvatar(), VolleyTool.getImageLoader());
    }

    private void showLayout3(Holder holder, ArrayList<TopicBean.DigestComment> comments) {
        holder.avatorLayout3.setVisibility(View.VISIBLE);
        holder.avtar3.setDefaultImageResId(R.mipmap.empty_photo);
        holder.avtar3.setImageUrl(Constants.IMAGE_HOST + comments.get(2).getAuthor().getAvatar(), VolleyTool.getImageLoader());
    }


    private class Holder {
        TextView time;
        NoScrollGridView gridView;
        RelativeLayout avatorLayout1;
        RelativeLayout avatorLayout2;
        RelativeLayout avatorLayout3;
        NetworkImageView avtar1;
        NetworkImageView avtar2;
        NetworkImageView avtar3;
        TextView joinCount;
        TextView comment;
        TextView content;
        ImageView typeIcon;
        ImageView imageView;
        ImageView tsType;
        LinearLayout commentLayout;
        LinearLayout commentLayout1;
    }

}
